package com.oxy.util;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.oxy.common.util.FileUtil;

public class Oxy {
	private volatile static Oxy inst;
	
	private ConfigManager cfg;
	private LogManager log;
	
	// singleton
	private Oxy() {
		__init();
	}
	private void __init() {
		// must have a fixed cfg entry
		String cfgFile = FileUtil.getSafePath(FileUtil.getClassPathAbsolute()+File.separator+"application.properties");
		cfg = new ConfigManager(cfgFile);
		log = new LogManager(cfg);
		
		dumpConfig();
	}
	public static void init() {
		if(inst == null) {
			synchronized(Oxy.class) {
				inst = new Oxy();
			}
		}
	}
	public static Oxy getInstance() {
		init();
		return inst;
	}
	
	public static String getAppName() {
		return getInstance().cfg.App().APP_NAME;
	}
	public static String getRootUrl() {
		return getInstance().cfg.App().ROOTURL;
	}
	public static String getRootPath() {
		return getInstance().cfg.App().ROOTPATH;
	}
	public static String getClassPath() {
		return getInstance().cfg.App().CLASSPATH;
	}
	public static String getLocale() {
		return getInstance().cfg.Struts().LOCALE;
	}
	
	// get item
	public static ConfigManager getCfgManager() {
		return getInstance().cfg;
	}
	public static LogManager getLogManager() {
		return getInstance().log;
	}
	
	private void dumpConfig() {
		Logger logger = log.getLoggerCommon();
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("Dump of configurations:\r\n");

			sb.append("[system]\r\n");
			Field[] flds = ConfigManager.AppCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfg.App());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[system - raw]\r\n");
			Properties prop = cfg.App();
			Enumeration keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
			
			sb.append("\r\n[database]\r\n");
			flds = ConfigManager.DbCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfg.Db());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[database - raw]\r\n");
			prop = cfg.Db();
			keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
			
			sb.append("\r\n[struts]\r\n");
			flds = ConfigManager.StrutsCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfg.Struts());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[struts - raw]\r\n");
			prop = cfg.Struts();
			keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
			
			sb.append("\r\n[logging]\r\n");
			flds = ConfigManager.LogCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfg.Log());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[logging - raw]\r\n");
			prop = cfg.Log();
			keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
			sb.append("Error when dump configurations.");
		}
		logger.debug(sb.toString());
	}
}
